package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Department;
import com.market.service.DepartmentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  KanShan
 * @version : 1.8
 * @Date: 2021/12/01/15:32
 * @注释 :
 */
@RestController
public class DepartmentController {
    @Resource
    DepartmentService departmentService;

    /**
     * 展示部门信息
     * @return
     * @throws ControllerException
     */
    @GetMapping("/department/find")
    public ResponseResult<List<Department>> findDepartment()throws ControllerException{
        List<Department> department = departmentService.findDepartment();
        return ResponseResult.success(ResponseCode.SUCCESS,department);

    }

    /**
     * 根据部门ID 部门名模糊查询部门信息
     * @param string
     * @return
     * @throws ControllerException
     */
    @GetMapping("/department/select")
    public ResponseResult<List<Department>>findDepartmentByIdOrName(String string)throws ControllerException{
        List<Department> departmentByIdOrName = departmentService.findDepartmentByIdOrName(string);
        return ResponseResult.success(ResponseCode.SUCCESS,departmentByIdOrName);

    }

    /**
     * 一对多 展示部门员工信息
     * @return
     * @throws ControllerException
     */
    @GetMapping("/department/find/list")
    public ResponseResult<List<Department>>find()throws  ControllerException{
        List<Department> departments = departmentService.find();
        return ResponseResult.success(ResponseCode.SUCCESS,departments);
    }

    /**
     * 添加部门信息
     * @param department
     * @return
     * @throws ControllerException
     */
    @PostMapping("/department/insert")
    public ResponseResult<Integer>insertDepartment(@RequestBody Department department) throws ControllerException{
        int i = departmentService.insertDepartment(department);
        return ResponseResult.success(ResponseCode.SUCCESS,i);
    }

    /**
     * 删除部门
     * @param id
     * @return
     * @throws ControllerException
     */
    @PostMapping("/department/delete")
    public ResponseResult<Integer>deleteDepartment(@RequestParam Long id) throws ControllerException{
        int i = departmentService.deleteDepartment(id);
        return ResponseResult.success(ResponseCode.SUCCESS,i);
    }

    /**
     * 修改部门信息
     * @param department
     * @return
     * @throws ControllerException
     */
    @PostMapping("/department/update")
    public ResponseResult<Integer>updateDepartment(@RequestBody Department department) throws ControllerException{
        int i = departmentService.updateDepartment(department);
        return ResponseResult.success(ResponseCode.SUCCESS,i);
    }
}